package com.cloudea.lotus.lambda;

import com.cloudea.lotus.Request;
import com.cloudea.lotus.Response;
import com.cloudea.lotus.action.LotusAction;
import com.cloudea.lotus.dao.DAO;

import java.util.HashMap;
import java.util.Map;

public class LotusLambdaExtend extends LotusLambda {
    private Map<String, LotusAction> actionMap = new HashMap<>();
    private DAO dao;

    public LotusLambdaExtend(DAO dao){
        this.dao = dao;
    }

    @Override
    public Response post(Request request) {
        Map<String, String> args = request.getParameters();
        if(!args.containsKey("action")){
            return Response.fail("请传递action");
        }

        String action = args.remove("action");
        if(!actionMap.containsKey(action)){
            return Response.fail("当前程序不存在此action: " + action);
        }

        LotusAction lotusAction = actionMap.get(action);
        return lotusAction.done(request, this.dao);
    }

    /**
     * 添加Action
     * @param actionName action的名字
     * @param lotusAction 要添加的action
     */
    public void addAction(String actionName, LotusAction lotusAction){
        if(actionName != null && !actionName.equals("") && lotusAction != null){
            actionMap.put(actionName, lotusAction);
        }
    }

    /**
     * 获取action
     * @param actionName
     * @return
     */
    public LotusAction getAcion(String actionName){
        return actionMap.get(actionName);
    }
}
